﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeBusiness.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;

namespace OfficeBusiness.ViewModel
{
    public class WorksViewModel : ViewModelBase
    {
        private IBusinessModel businessModel;
        private ObservableCollection<ViewModel.Work> worksData;
        private Work selectedItem;
        private ICommand createCommand;
        private ICommand refreshCommand;
        private ICommand deleteRowCommand;
        private ICommand updateCommand;

        public WorksViewModel(IBusinessModel businessModel)
        {
            this.businessModel = businessModel;
            this.businessModel.WorksSelectedEvent += new EventHandler(WorkSelected);
        }

        private void WorkSelected(object workVisibility, EventArgs e)
        {
            this.WorksData.Clear();
            this.GetWorkDetails();
        }

        private void GetWorkDetails()
        {
            var workList = this.businessModel.GetWorksDetails();
            foreach (var empItem in workList)
            {
                this.WorksData.Add(empItem);
            }
        }

        public ObservableCollection<ViewModel.Work> WorksData
        {
            get
            {
                if (this.worksData == null)
                {
                    this.worksData = new ObservableCollection<Work>();
                }

                return this.worksData;
            }
        }

        public Work SelectedItem
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                if (this.createCommand == null)
                {
                    this.createCommand = new RelayCommand(param => this.CreateRow(), param => true);
                }

                return this.createCommand;
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                if (this.refreshCommand == null)
                {
                    this.refreshCommand = new RelayCommand(param => this.RefreshTable(), param => true);
                }

                return this.refreshCommand;
            }
        }

        public ICommand DeleteRowCommand
        {
            get
            {
                if (this.deleteRowCommand == null)
                {
                    this.deleteRowCommand = new RelayCommand(param => this.DeleteRow(), param => true);
                }

                return this.deleteRowCommand;
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                if (this.updateCommand == null)
                {
                    this.updateCommand = new RelayCommand(param => this.Update(), param => true);
                }

                return this.updateCommand;
            }
        }

        private void Update()
        {
            var worksList = from e in this.WorksData select e;
            bool updateStatus = this.businessModel.UpdateRows<ViewModel.Work>(worksList.ToList());
            if (!updateStatus)
            {
                MessageBox.Show("Update failed");
                this.RefreshTable();
            }
        }

        private void DeleteRow()
        {
            if (this.SelectedItem != null)
            {
                bool deleteStatus = this.businessModel.DeleteSelectedRow<ViewModel.Work>(this.SelectedItem);
                if (!deleteStatus)
                {
                    MessageBox.Show("Delete failed");
                }
                else
                {
                    this.WorksData.Remove(this.SelectedItem);
                }
            }
            else
            {
                MessageBox.Show("Please select a row");
            }
        }

        private void CreateRow()
        {
            var worksList = this.businessModel.GetWorksDetails();
            var newItems = from itms in this.WorksData where !((from exItems in worksList select exItems.ProjectNumber).Contains(itms.ProjectNumber) && (from exItems in worksList select exItems.EmployeeNumber).Contains(itms.EmployeeNumber)) select itms;
            bool createStatus = this.businessModel.InsetRows<ViewModel.Work>(newItems.ToList());
            if (!createStatus)
            {
                MessageBox.Show("Create failed");
                this.RefreshTable();
            }
        }

        private void RefreshTable()
        {
            this.WorksData.Clear();
            this.GetWorkDetails();
        }
    }
}
